
Create PROC [dbo].[STISendInvoicesToTaxSystem]
    @ComputerName NVARCHAR(MAX),
    @InvoiceType TINYINT,
    @Userid SMALLINT
AS
BEGIN
    DECLARE @rowcount INT =
            (
                SELECT COUNT(*)
                FROM ##CacheFactor WITH (NOLOCK)
                WHERE IsSend = 1
                      AND ComputerName = @ComputerName
            );
    IF (@rowcount > 0)
       AND (@InvoiceType IN ( 7, 5, 9 ,1))
    BEGIN
	DECLARE @InvoiceTypeSale TINYINT = (CASE
                                                  WHEN @InvoiceType in ( 7, 5, 9 ) THEN
                                                      5
                                                  WHEN @InvoiceType = 1 THEN
                                                       0
                                               else
											     -1
                                              END
                                             );
        INSERT INTO dbo.STITransaction
        (
            InvoiceId,
            Type,
            Status,
            CreatedBy
        )
        SELECT Id,
               @InvoiceTypeSale,
               0,
               @Userid
        FROM ##CacheFactor WITH (NOLOCK)
        WHERE (ComputerName = @ComputerName)
              AND (InvoiceType = @InvoiceType)
              AND (issend = 1);

        DELETE FROM ##CacheFactor
        WHERE (ComputerName = @ComputerName)
              AND (InvoiceType = @InvoiceType)
              AND (issend = 1);
        RETURN @rowcount;
    END;
    IF @InvoiceType IN ( 8, 0, 6,2 )
       AND (@rowcount > 0)
    BEGIN
        DECLARE @InvoiceTypeRetuen TINYINT = (CASE
                                                  WHEN @InvoiceType = 8 THEN
                                                      7
                                                  WHEN @InvoiceType = 0 THEN
                                                      9
                                                  WHEN @InvoiceType = 6 THEN
                                                      5
                                                  WHEN @InvoiceType = 2 THEN
                                                      1
                                                  ELSE
                                                      -1
                                              END
                                             );
--------------------AccReturn------------
        if @InvoiceType in ( 8, 0, 6)
		begin
        INSERT INTO dbo.STITransaction
        (
            InvoiceId,
            Type,
            Status,
            CreatedBy
        )
        SELECT tbReturn.Id,
               6,
               0,
               @Userid
        FROM
        (
            SELECT Id,
                   invoiceNumber
            FROM ##CacheFactor WITH (NOLOCK)
            WHERE (
                      (ComputerName = @ComputerName)
                      AND (InvoiceType = @InvoiceType)
                      AND (issend = 1)
                  )
                  AND invoicenumber IN
                      (
                          SELECT Nu_Bargasht_Forosh
                          FROM headH WITH (NOLOCK)
                              INNER JOIN ##CacheFactor
                                  ON ##CacheFactor.Id = headH.Id_Havaleh
                          WHERE Nu_Forosh IS NOT NULL
                                AND Kind_Flag = @InvoiceType
                      )
        ) AS tbReturn
            INNER JOIN
            (
                SELECT Nu_Bargasht_Forosh,
                       Nu_Forosh,
                       Kind_Flag
                FROM headH WITH (NOLOCK)
                    INNER JOIN ##CacheFactor
                        ON ##CacheFactor.Id = headH.Id_Havaleh
                WHERE (
                          (ComputerName = @ComputerName)
                          AND (InvoiceType = @InvoiceType)
                          AND (issend = 1)
                      )
                      AND Nu_Forosh IS NOT NULL
                      AND Kind_Flag = @InvoiceType
                      AND Nu_Forosh IN
                          (
                              SELECT Nu_Forosh
                              FROM dbo.STITransaction WITH (NOLOCK)
                                  INNER JOIN headH
                                      ON headH.Id_Havaleh = dbo.STITransaction.InvoiceId
                                         AND headH.Kind_Flag = @InvoiceTypeRetuen
                                         AND Nu_Forosh IN
                                             (
                                                 SELECT Nu_Forosh
                                                 FROM headH WITH (NOLOCK)
                                                 WHERE Kind_Flag = @InvoiceType
                                                       AND Nu_Forosh IS NOT NULL
                                             )
                              WHERE Type IN ( 5 )
                                    AND Status = 1
                          )
            ) AS tbSale
                ON tbSale.Nu_Bargasht_Forosh = tbReturn.invoiceNumber;
				 RETURN @@ROWCOUNT;
				 End
-------------------------------------DrugReturn-------------------------
		if @InvoiceType = 2 
		begin
		 INSERT INTO dbo.STITransaction
        (
            InvoiceId,
            Type,
            Status,
            CreatedBy
        )
        SELECT tbReturn.Id,
               1,
               0,
               @Userid
        FROM
        (
            SELECT Id,
                   invoiceNumber
            FROM ##CacheFactor WITH (NOLOCK)
            WHERE (
                      (ComputerName = @ComputerName)
                      AND (InvoiceType = @InvoiceType)
                      AND (issend = 1)
                  )
                  AND invoicenumber IN
                      (
                          SELECT Nu_Bargasht_Forosh
                          FROM Drugh WITH (NOLOCK)
                              INNER JOIN ##CacheFactor
                                  ON ##CacheFactor.Id = Drugh.Id_Havaleh
                          WHERE Nu_Forosh IS NOT NULL
                                AND Kind_Flag = @InvoiceType
                      )
        ) AS tbReturn
            INNER JOIN
            (
                SELECT Nu_Bargasht_Forosh,
                       Nu_Forosh,
                       Kind_Flag
                FROM Drugh WITH (NOLOCK)
                    INNER JOIN ##CacheFactor
                        ON ##CacheFactor.Id = Drugh.Id_Havaleh
                WHERE (
                          (ComputerName = @ComputerName)
                          AND (InvoiceType = @InvoiceType)
                          AND (issend = 1)
                      )
                      AND Nu_Forosh IS NOT NULL
                      AND Kind_Flag = @InvoiceType
                      AND Nu_Forosh IN
                          (
                              SELECT Nu_Forosh
                              FROM dbo.STITransaction WITH (NOLOCK)
                                  INNER JOIN Drugh
                                      ON Drugh.Id_Havaleh = dbo.STITransaction.InvoiceId
                                         AND Drugh.Kind_Flag = @InvoiceTypeRetuen
                                         AND Nu_Forosh IN
                                             (
                                                 SELECT Nu_Forosh
                                                 FROM Drugh WITH (NOLOCK)
                                                 WHERE Kind_Flag = @InvoiceType
                                                       AND Nu_Forosh IS NOT NULL
                                             )
                              WHERE Type IN ( 0 )
                                    AND Status = 1
                          )
            ) AS tbSale
                ON tbSale.Nu_Bargasht_Forosh = tbReturn.invoiceNumber;
				 RETURN @@ROWCOUNT;
		end
        DELETE FROM ##CacheFactor
        WHERE (ComputerName = @ComputerName)
              AND (InvoiceType = @InvoiceType)
              AND (issend = 1);
	 
    END;

    ELSE
        RETURN 0;

END;
